var app = getApp();
Page({
  data: {
    isPassWord: true,
    pwd_val: '',
    btnValue:'获取验证码',
    phone:"",
    second: 60,
    btnDisabled:false,
    code:'',
    zcxy:1
  },

  onLoad: function (options) {

  },

  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    if(e.detail.value[0]==1){
      this.setData({
        zcxy:1
      })
      console.log(1)
    }else{
      this.setData({
        zcxy:0
      })
      console.log(0)
    }
  },

  tijiao:function(){
    var t = this;
    if (!t.data.phone){
      app.myToast("请输入手机号");
    } else if (!(/^1[3456789]\d{9}$/.test(t.data.phone))){
      app.myToast("手机号格式有误");
    }else if(!t.data.code){
      app.myToast("请输入验证码");
    }else if(!t.data.pwd_val){
      app.myToast("请输入新密码");
    }else if(t.data.pwd_val.length < 8){
      app.myToast("密码长度大于八位");
    }else if(t.data.zcxy==0){
      app.myToast("请勾选注册协议");
    }else{
      app.visit("register/registerStoreYzm",
          {
            code: t.data.code,
            password: t.data.pwd_val,
            phone: t.data.phone,
          }, "", function (res) {
            console.log("注册=", res)
            if (res.code==0) {
              app.showSuccess("商家注册成功，请登录完善商家信息",wx.reLaunch({
                url: 'index'
              }));
            } else {
              app.myToast(res.msg)
            }
          })
    }
  },

  xieyi:function(){
    wx.navigateTo({
      url: 'zhucxy'
    })
  },

  phone:function(e){
    var phone = e.detail.value;
    console.log(phone)
    this.setData({
      phone:phone
    })
  },

  //验证码输入
  bindCodeInput:function(e) {
    console.log(e.detail.value)
    this.setData({
      code: e.detail.value
    })
  },

  /**
   * 获取验证码
   ******/
  sys: function (options) {
    let t = this;
    console.log(options.currentTarget.dataset);
    let phone = options.currentTarget.dataset.phone;
    if (phone == ''){
      wx.showToast({
        title: "请先输入手机号~",
        icon: "none",
        success: function () {
          setTimeout(function () {
            wx.hideToast();
          }, 1e3);
        }
      });
    } else if (!(/^1[3456789]\d{9}$/.test(phone))){
      wx.showToast({
        title: "请输入的手机号有误~",
        icon: "none",
        success: function () {
          setTimeout(function () {
            wx.hideToast();
          }, 1e3);
        }
      });
    }else{
      app.visit('register/code',
          {
            phone: options.currentTarget.dataset.phone,
          },
          "",
          function (res) {
            console.log(res);
            t.setData({
              display: "none",
            });
            t.timer();
            wx.showToast({
              title: res.msg,
              icon: "none",
              success: function () {
                setTimeout(function () {
                  wx.hideToast();
                }, 1e3);
              }
            });
          });
    }
  },

  timer: function () {
    let promise = new Promise((resolve, reject) =>{
      let setTimer = setInterval(
          () =>{
            var second = this.data.second - 1;
            this.setData({
              second: second,
              btnValue: second+'秒',
              btnDisabled: true
            })
            if (this.data.second <= 0) {
              this.setData({
                second: 60,
                btnValue: '获取验证码',
                btnDisabled: false
              })
              resolve(setTimer)
            }
          }
          , 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },

  bindblur: function (e) {
    console.log(e.detail.value)
    this.setData({
      pwd_val: e.detail.value
    })
  },
  isShow: function () {
    this.setData({
      isPassWord: !this.data.isPassWord
    })
  },

})